package com.goldze.mvvmhabit.ui.card;

import android.app.Application;

import com.goldze.mvvmhabit.BR;
import com.goldze.mvvmhabit.entity.CartInfoEntity;
import com.goldze.mvvmhabit.widght.swiperv.SwipeItemBinding;
import com.goldze.mvvmhabit.widght.swiperv.SwipeOnItenBind;

import java.util.ArrayList;
import java.util.List;
import com.goldze.mvvmhabit.R;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;

/**
 * create om  2020/11/16.
 * Created by  gaoxuge
 * email android_gaoxuge@163.com
 * 功能描述
 */
public class CardViewModel extends BaseViewModel {
    public CardViewModel(Application application) {
        super(application);
    }
    public ObservableField<Boolean> showEmptyViewtest = new ObservableField<>(true);
    public ObservableField<String> rightTitleLabel = new ObservableField<>("编辑");
    public SingleLiveEvent<ObservableList<ItemViewModel>> refreshList = new SingleLiveEvent<>();
    public SwipeItemBinding<CartItemTestViewModel> itemBinding = SwipeItemBinding.of(new SwipeOnItenBind<CartItemTestViewModel>() {
        @Override
        public void onItenBind(SwipeItemBinding<CartItemTestViewModel> itemBinding, int pisition, CartItemTestViewModel item) {
            itemBinding.set(com.goldze.mvvmhabit.BR.viewModel, R.layout.cart_item_test_layout);
        }
    });
    public ObservableList<ItemViewModel> list = new ObservableArrayList<>();

    public BindingCommand onRightTitleLabelClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (rightTitleLabel.get().equals("编辑")) {
                rightTitleLabel.set("完成");
            } else {
                rightTitleLabel.set("编辑");
            }
        }
    });
    public void getCardInfo(boolean resetView) {
        CartInfoEntity data = new CartInfoEntity();
        List<CartInfoEntity.NoProductsManjian> products =new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            CartInfoEntity.NoProductsManjian productsManjian = new  CartInfoEntity.NoProductsManjian();
            productsManjian.setName("商品"+(i+1));
            productsManjian.setMain_image("https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1176503144,1033294539&fm=26&gp=0.jpg");
            products.add(productsManjian);
        }
        data.setNoProductsManjian(products);
        initData(data,resetView);

//        addSubscribe(model.cartInfo()
//                .compose(RxUtils.schedulersTransformer())
//                .doOnSubscribe(new Consumer<Disposable>() {
//                    @Override
//                    public void accept(Disposable disposable) throws Exception {
//                        showDialog();
//                    }
//                })
//                .subscribe(new Consumer<BaseResponseEntity>() {
//                    @Override
//                    public void accept(BaseResponseEntity baseResponseEntity) {
//                        dismissDialog();
//                        if (baseResponseEntity.isOk()) {
////                            CartService.getInstance().getFlashCart();
//                            initData((CartInfoEntity) baseResponseEntity.getData(), resetView);
//                        }
//                    }
//                }));
    }

    private void initData(CartInfoEntity data, boolean resetView) {
        list.clear();
        int index= 0;
        if(data.getNoProductsManjian()!=null && data.getNoProductsManjian().size()>0){
            showEmptyViewtest.set(false);
            for (CartInfoEntity.NoProductsManjian noProductsManjian : data.getNoProductsManjian()) {
                CartItemTestViewModel cartItemViewModel = new CartItemTestViewModel(CardViewModel.this, noProductsManjian,index);
                index++;
                list.add(cartItemViewModel);
            }
        }else{
            showEmptyViewtest.set(true);
        }
        refreshList.setValue(list);
        KLog.e("sss  "+list.size());

    }
}
